package com.ddn.controller.hall;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.FileUtils;
import com.ddn.model.hall.BathBo;
import com.ddn.service.hall.BathService;


import com.ddn.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;

/*
  ❀❀❀---扎根---❀❀❀
    ❤ @libing ❤
  
*/
@RestController
@RequestMapping("/bath")
public class BathController {

    @Autowired
    private BathService bathService;

    /**
     * 查找全部的洗浴信息
     * @param map
     * @return
     */
    @RequestMapping("/list")
    public ResultData listBath(@RequestParam HashMap<String, Object> map) {
        ResultData resultData = bathService.findAllBath(map);
        return resultData;
    }

    /**
     * 删除洗浴信息
     * @param map
     * @return
     */
    @RequestMapping("/delete")
    public ResultData deleteBath(@RequestParam HashMap<String, Object> map) {
        ResultData resultData = bathService.deleteBath(map);
        return resultData;
    }

    /**
     * 增加洗浴信息
     * @param map
     * @return
     */
    @RequestMapping("/add")
    public ResultData addBath(@RequestParam HashMap<String, Object> map) {
        ResultData resultData = bathService.addBath(map);
        return resultData;
    }

    /**
     * 增加洗浴信息
     * @param map
     * @return
     */
    @RequestMapping("/update")
    public ResultData updateBath(@RequestParam HashMap<String, Object> map) {
        ResultData resultData = bathService.updateBath(map);
        return resultData;
    }

    /**
     * 批量删除洗浴信息
     * @param ids
     * @return
     */
    @RequestMapping("/batch")
    public ResultData batchBath(@RequestParam List<Integer> ids) {
        System.out.println(ids);
        ResultData resultData = bathService.batchBath(ids);
        return resultData;
    }

    /**
     * 导入表格
     * @param
     * @return
     */
    @RequestMapping("/import")
    public ResultData importBath(MultipartFile file) throws IOException {
        ResultData resultData = bathService.importBath(file);
        return resultData;
    }

    /**
     * 导出表格模板
     * @param
     * @return
     */
    @RequestMapping("/export")
    public void exportBath(HttpServletResponse response) throws IOException {
        //1设置消息响应头
        response.setHeader("content-disposition","attachment;filename= bath" +System.currentTimeMillis()+".xlsx");
        //2.查询数据库中的数据
        List<BathBo> list=bathService.exportAllBath();
        //3.实现数据导出Excel格式
        EasyExcel.write(response.getOutputStream(),
                BathBo.class).
                sheet(System.currentTimeMillis()+"").
                doWrite(list);
        response.getOutputStream().flush();
    }

    /**
     * 下载 excel 模板文件
     */
    @RequestMapping("/templateDownload")
    public void templateDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //设置下载excel文件的响应头
        response.setContentType("application/vnd.ms-excel"); //  ===>   response.setHeader("content-type","");
        response.setCharacterEncoding("utf-8");

        String realPath = request.getServletContext().getRealPath("/洗浴信息模板.xlsx");

        File file = new File(realPath);

        //把file 对象转为字节数组
        byte[] bytes = FileUtils.readFileToByteArray(file);

        //下载的文件的名字 是中文 进行转码
        String filename = URLEncoder.encode("洗浴信息模板", "utf-8");

        //告诉浏览器下载文件
        response.setHeader("Content-Disposition", "attachment;filename=" + filename + ".xlsx");

        //得到指向浏览器的输出流
        ServletOutputStream outputStream = response.getOutputStream();

        outputStream.write(bytes);

        outputStream.close();
    }
}
